package com.zy.algorithm.v3.array

import com.zy.view.DIYSortView
import com.zy.utils.data.Point
import com.zy.utils.data.auto.AutoIndexElement
import com.zy.utils.data.auto.AutoNodeStep
import com.zy.utils.data.auto.Coordinate
import com.zy.zlog.ZLog
import java.util.ArrayList

//https://leetcode.cn/problems/two-sum/
open class LeetCode0001_2 : BaseV3ArrayFragment() {
    private var array1 = intArrayOf(-11, 7, 3, 2, 1, 7, -10, 11, 21, 3)
    private var array2 = intArrayOf(11)

    private val baseIX = 2

    private val ass1 = builder.setOriginX(baseIX).setOriginY(3).setV("^").setIndex(999)
        .build(Point.Type.SUB_SCRIPT)

    private val indexI =
        builder.setOriginX(1).setOriginY(3).setV("i=0").setIndex(555).setCircle(true)
            .build(Point.Type.DEFAULT)

    private val nodeI =
        builder.setOriginX(baseIX).setOriginY(4).setV("-11").setIndex(777).build(Point.Type.DEFAULT)


    private val sumNode =
        builder.setOriginX(13).setOriginY(4).setV("22").setIndex(333).build(Point.Type.DEFAULT)


    override fun getInitD(): Int {
        return 150
    }

    override fun setData(diyV4: DIYSortView) {
        title.text = "方法二:哈希表"
        //diyV4.changeType = 1
        diyV4.setD(getInitD())

        diyV4.create(2, 2, array1, Point.Type.DEFAULT)
        diyV4.create(13, 2, array2, Point.Type.DEFAULT)

        //diyV4.addNoMoveAssistList(1, 5, array2, Point.Type.DEFAULT)

        //diyV4.addAss(1, 1, "V")
        addAss()
    }

    var hasMark = false

    private fun indexAddAdd() {
        if (hasMark) {
            return
        }

        i++
        if (i >= array1.size) {
            return
        }

        val otherTarget = 11 - array1[i]

        if (map.containsKey(otherTarget)) {
            ZLog.d("LeetCode0001_2", "已匹配到两数之和")
            hasMark = true

        } else {
            diyV4.addPoint(
                builder.setOriginX(baseIX + i).setOriginY(5).setV("${array1[i]}=$i").setIndex(222)
                    .setVTextSize(48).build(Point.Type.DEFAULT)
            )
        }

        //记录
        map[array1[i]] = i

        val list: MutableList<AutoNodeStep> = ArrayList<AutoNodeStep>()

        //下标
        indexI.v = "i=$i"

        //数值
        nodeI.v = array1[i].toString()
        sumNode.v = (otherTarget).toString()


        val stepList = listOf(
            AutoIndexElement(999, Coordinate(baseIX + i, 3)),
            AutoIndexElement(777, Coordinate(baseIX + i, 4))
        )
        list.add(AutoNodeStep(stepList))

        diyV4.autoMove(list)
    }


    //初始i下标为0
    private var i = -1
    private val map = mutableMapOf<Int, Int>()

    override fun autoClick() {
        //下一步
        indexAddAdd()
    }

    private fun addAss() {
        diyV4.addPoint(ass1)

        diyV4.addPoint(nodeI)

        diyV4.addPoint(indexI)

        diyV4.addPoint(sumNode)
        diyV4.addPoint(
            builder.setOriginX(12).setOriginY(4).setV("=").setIndex(222)
                .build(Point.Type.SUB_SCRIPT)
        )
    }
}